package com.atom.common.activiti.diagram;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.atom.common.DiagramType;
import com.atom.common.dao.DiagramUrlDao;
import com.atom.common.dao.TaskDefinitionDao;
import com.atom.common.entity.DiagramUrlDO;
import com.atom.common.entity.base.TaskDefinitionDO;
import com.atom.common.entity.bo.TaskDiagramBO;
import com.atom.common.exception.AvensFileException;
import com.atom.common.remote.file.RemoteFileCommand;
import com.atom.common.remote.file.RemoteFileDao;
/**
 * Title: HTSDDiagramHandler
 * Description: 生成查看流程图所需数据
 * @author WEWE
 * @date 2016年8月11日 下午4:13:30
 * @version 1.0
 */
@Component
public class DiagramHandler {

  @Resource TaskDefinitionDao taskDefinitionDao;
  @Resource UserTaskDiagramHandler userTaskDiagramHandler;
  @Resource CommentDiagramHandler commentDiagramHandler;
  @Resource DiagramUrlDao diagramUrlDao;
  @Resource RemoteFileDao remoteFileDao;
  @Resource RemoteFileCommand command;

  public Map<String, TaskDiagramBO> getDiagram(String mainProcessInstanceId, String processDefinitionKey) {
    Map<String, TaskDiagramBO> data = new HashMap<String, TaskDiagramBO>();
    List<TaskDefinitionDO> taskDefs = taskDefinitionDao.getTaskDefinitionList(processDefinitionKey);
    // 遍历任务定义
    for (TaskDefinitionDO taskDef : taskDefs) {
      if (taskDef.getDiagramType() == DiagramType.USERTASK) {
        String taskDefKey = taskDef.getTaskDefinitionKey();
        TaskDiagramBO diagram = userTaskDiagramHandler.getUserTaskDiagram(taskDefKey, mainProcessInstanceId);
        data.put(taskDefKey, diagram);
      } else if (taskDef.getDiagramType() == DiagramType.COMMENT) {  
        String taskDefKey = taskDef.getTaskDefinitionKey();
        TaskDiagramBO diagram = commentDiagramHandler.getCommentDiagram(taskDefKey, mainProcessInstanceId);
        data.put(taskDefKey, diagram);
      }       
    }   
    for (TaskDefinitionDO taskDef : taskDefs) {
      if (taskDef.getDiagramType() == DiagramType.USERTASK
          && taskDef.getSuperTaskKey() != null
          && taskDef.getSuperTaskKey().length() != 0) {
        String superTaskDefKey = taskDef.getSuperTaskKey();
        TaskDiagramBO superDiagram = data.get(superTaskDefKey);
        TaskDiagramBO diagram = data.get(taskDef.getTaskDefinitionKey());
        if (diagram.getState() > superDiagram.getState()) {
          superDiagram.setState(diagram.getState());
          data.put(superTaskDefKey, superDiagram);
        }
      }       
    }   
    return data;
  }
  
  public File getHtml(String mainProcessInstanceId) {
    List<DiagramUrlDO> fileList = diagramUrlDao.findByProperty("mainProcessInstanceId", mainProcessInstanceId);
    if (fileList == null || fileList.size()!= 1) {
      throw new AvensFileException("Find file key by mainProcessInstanceId failed where mainProcessInstanceId = " + mainProcessInstanceId);
    }

    String url = fileList.get(0).getHtmlUrl();
    String filePath = command.downloadFile(url);
    File file = new File(filePath);
    if (file.exists()) {
      return file;
    }
    return null;
  }

  public boolean isHtml(String mainProcessInstanceId) {
    List<DiagramUrlDO> fileList = diagramUrlDao.findByProperty("mainProcessInstanceId", mainProcessInstanceId);
    if (fileList == null || fileList.size()== 0) {
      return false;    
    }
    return true;
  }

}
